// pages/addressList/addressList.js
var province = require('../../province.js');
// var confirm = require('../../confirm/confirm.js');
var QQMapWX = require("../../utils/qqmap-wx-jssdk.js");
var qqmapsdk;
var app = getApp(),
  fn = app.fn;

Page({
  data: {
  
    newShow: true,
    ajxtrue: true,
    addId: '',
    consignee: '',
    addressName: '',
    latitude: '',
    longitude: '',
    addressPhone: '',
    addressDetailed: '',
    provinceId: '',
    cityId: '',
    areaId: '',
    postcodes: '000000',
    address: [],
    provinceArray: [],
    cityArray: [],
    countyArray: [],
    provinceIndex: '',
    cityIndex: '',
    countyIndex: '',
    provinceName: '',
    cityName: '',
    countyName: '',
    addType: 0,
    // 
    block: 0,
  },
  ProID: '',
  CityID: '',
  AreID: '',
  cityArrayDemo: [],
  countyArrayDemo: [],

  onLoad: function (options) {


    console.log(JSON.stringify(options) + "bbbbbbbbbbbbbbb");
    this.setData({
      'address_id': options.address_id,
    })


    this.getInitData(options);
 
  
  
    // this.getprovinceData();

   
    this.setData({
      block: options.buycode,
    });
  },


  onPullDownRefresh: function () {

  },

  getprovinceData: function () {
    var provinceArrayDemo = []
    for (var i = 0; i < province.provinceArray.length; ++i) {
      var item = province.provinceArray[i]
      provinceArrayDemo.push(item.name)
    }
    this.setData({
      provinceArray: provinceArrayDemo
    })
  },


  getInitData: function (e) {
    var that = this;
   
    fn.apiData({
      url: 'user.php',
      data: {
        act:'get_address',
        user_id: app.globalData.uid,
        address_id: e.address_id,
      },
      success: function (res) {
        if (res.data.code==0) {
          that.setData({ 
            'address': res.data.data,
            'consignee': res.data.data.consignee.consignee,
            'addressPhone': res.data.data.consignee.mobile,
            'addressDetailed': res.data.data.consignee.house_number,
            'addressName': res.data.data.consignee.address1,
            'latitude': res.data.data.consignee.lat,
            'longitude': res.data.data.consignee.lng
           });
      
          wx.hideToast();
        }
      }
    });
  },


  bindSelect: function (e) {
    var that = this;

    var address = that.data.address;
    var aid = e.currentTarget.dataset.id;
    var block = that.data.block;

    if (block == 1) {
    
      for (var i = 0; i < address.length; i++) {
        var addInfo = address[i];
        if (addInfo.addId == aid) {
     
          var pagesLength = getCurrentPages().length;
       
          var frontPage = getCurrentPages()[pagesLength - 2];
          frontPage.addressBlock(addInfo);
        }
      }
    
      wx.navigateBack({
        delta: 1
      })
    }
  },

 
  defaultClick: function (e) {
    var that = this;
    that.setData({
      addId: e.currentTarget.dataset.id
    });

    fn.apiData({
      url: 'selectAddress.php',
      data: {
        uid: that.data.uid,
        addId: that.data.addId
      },
      success: function (res) {

        wx.showToast({
          title: res.data.error_msg,
          icon: 'success',
          duration: 1000
        })
        if (res.data.success) {
          that.setData({ 'address': [] });
          that.getInitData();
        }
      }
    });
  },


  deleteClick: function (e) {
    var that = this;
    that.setData({
      addId: e.currentTarget.dataset.id
    });
    wx.showModal({
      title: '提示',
      content: '确定要删除该地址吗?',
      success: function (res) {
        if (res.confirm) {
  
        }
      }
    })

  },

  deleteData: function (e) {
    var that = this;
    fn.apiData({
      url: 'deleteAddress.php',
      data: {
        uid: that.data.uid,
        addId: that.data.addId
      },
      success: function (res) {

        wx.showToast({
          title: res.data.error_msg,
          icon: 'success',
          duration: 2000
        })
        if (res.data.success) {
          that.setData({ 'address': [] });
          that.getInitData();
        }
      }
    });
  },


  detailsClick: function (e) {

    var that = this;
    that.setData({
      'newShow': false,
      addId: e.currentTarget.dataset.id,
      addType: 1,
    });
    fn.apiData({
      url: 'addressDetail.php',
      data: {
        uid: that.data.uid,
        addId: that.data.addId
      },
      success: function (res) {
        if (res.data.success) {
          that.setData({

            addressName: res.data.result.name,
            addressPhone: res.data.result.tel,
            addressDetailed: res.data.result.address,
            provinceId: res.data.result.province,
            cityId: res.data.result.city,
            areaId: res.data.result.area,
            provinceName: res.data.result.provinceName,
            cityName: res.data.result.cityName,
            countyName: res.data.result.areaName,
            provinceIndex: '',
            cityIndex: '',
            countyIndex: '',
          });

          this.ProID = that.data.provinceId,
            this.CityID = that.data.cityId,
            this.AreID = that.data.areaId,

       
            wx.hideToast();

          var proIndex;
          for (var i = 0; i < province.provinceArray.length; ++i) {
            var item = province.provinceArray[i]
            if (item.ProID == that.data.provinceId) {
              proIndex = i;
              break
            }
          }

          this.cityArrayDemo = []
      
          for (var i = 0; i < province.cityArray.length; ++i) {
            var item = province.cityArray[i]
            if (item.ProID == that.data.provinceId) {
              this.cityArrayDemo.push(item.name)
            }
          }

          var citIndex;
          for (var i = 0; i < this.cityArrayDemo.length; ++i) {
            var item = this.cityArrayDemo[i]
            if (item == that.data.cityName) {
              citIndex = i;
              break
            }
          }

          this.countyArrayDemo = []
          for (var i = 0; i < province.countyArray.length; ++i) {
            var item = province.countyArray[i]
            if (item.CityID == that.data.cityId) {
              this.countyArrayDemo.push(item.DisName)
            }
          }

          var couIndex;
          for (var i = 0; i < this.countyArrayDemo.length; ++i) {
            var item = this.countyArrayDemo[i]
            if (item == that.data.countyName) {
              couIndex = i;
              break
            }
          }

          that.setData({
            provinceIndex: proIndex,
            cityIndex: citIndex,
            countyIndex: couIndex,
            cityArray: this.cityArrayDemo,
            countyArray: this.countyArrayDemo,
          });
        }
      }
    });
  
    this.animation.opacity(1).translateY(0).step();
  },


  newOpen: function () {

  
    var that = this;

    wx.chooseLocation({
      success: function (res) {
     
       
      
      
     

        var e = res.latitude, s = res.longitude, n = e + "," + s;
        qqmapsdk = new QQMapWX({
          key: 'XVZBZ-N4DK3-66C3A-3RJ2N-MH6JT-BUBGH'
        });
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: e,
            longitude: s
          },
          coord_type: 1,
          success: function (t) {
            var e = t.result.ad_info.location;
         
  
            var ddsd = t.result.address_component;
     
     
            var newaddress = "";
    
            if (res.address.indexOf('区') > 0) {
              newaddress = res.address;
           

            } else {

              newaddress = ddsd.province + ddsd.city + ddsd.district + res.name;
           
            }
         
            that.setData({
              'addressInfo': ddsd,
              addressName: newaddress,
              latitude: res.latitude,
              longitude: res.longitude
            })
           

          }
        });
    
      },
      fail: function () {
        // fail
      },
      complete: function () {
     
      }
    })
  

  },


  newClose: function () {

    this.animation.opacity(0).translateY(500).step();

    setTimeout(function () {
  
      this.setData({
        'newShow': true
      });
    }.bind(this), 300);
  },


  submitClick: function () {


      this.addClick();
  
  },


  editorClick: function () {
    var that = this;
    fn.apiData({
      url: 'eidtAddress.php',
      data: {
        uid: that.data.uid,
        addId: that.data.addId,
        name: that.data.addressName,
        tel: that.data.addressPhone,
        postCode: that.data.postcodes,
        province: that.data.provinceId,
        city: that.data.cityId,
        area: that.data.areaId,
        address: that.data.addressDetailed,
      },
      success: function (res) {

        wx.showToast({
          title: res.data.error_msg,
          icon: '',
          duration: 2000
        })

        if (res.data.success) {
          that.newClose();
          that.setData({ 'address': [] });
          that.getInitData();
        }
      }
    });
  },


  addClick: function () {
    var that = this;

    var address = that.data.address;
    var aid = that.data.address_id;

    if (that.data.ajxtrue) {

    fn.apiData({
      url: 'user.php',
      data: {
        location_status:'2',
        act:'update_address',
        address_id: that.data.address_id,
        user_id: app.globalData.uid,
        consignee: that.data.consignee,
        mobile: that.data.addressPhone,
        postCode: that.data.postcodes,
        province: that.data.provinceId,
        city: that.data.cityId,
        area: that.data.areaId,
        lng: that.data.longitude,
        lat: that.data.latitude,
        address: that.data.addressName,
        house_number: that.data.addressDetailed,
      },
      success: function (res) {

        wx.showToast({
          title: res.data.error_msg,
          icon: '',
          duration: 2000
        })



       
        if (res.data.code==0) {
          wx.showToast({
            title: '保存成功',
            icon: '',
            duration: 2000
          })
          if (that.data.block == 0) { 
            wx.navigateTo({
              url: '../myaddress/myaddress'
            })
          } else { 
            
            if (address.consignee.address_id == aid) {

                var pagesLength = getCurrentPages().length;

                var frontPage = getCurrentPages()[pagesLength - 3];

                frontPage.addressBlock(address.consignee);

              }
            wx.navigateBack({
              delta: 2
            });
          }
        }
      }
    });
    }else{

      wx.showToast({
        title: '手机号有误',
        icon: 'success',
        image: '/images/address_close.png',
        duration: 2000
      })


    }
  },



  bindName: function (e) {
    var phone = e.detail.value;






    if (isNaN(phone)) {

      this.setData({
        ajxtrue: false
      })



      wx.showToast({
        title: '手机号有误',
        icon: 'success',
        image: '/images/address_close.png',
        duration: 2000
      })


    } else {

      this.setData({
        ajxtrue: true
      })

      this.setData({
        addressPhone: e.detail.value
      })
    }


 
  },
  bindDetailed: function (e) {
    this.setData({
      addressDetailed: e.detail.value
    })
  },


  bindProvinceChange: function (e) {
  
    var name;
    for (var i = 0; i < province.provinceArray.length; ++i) {
      var item = province.provinceArray[i]
      if (i == e.detail.value) {
        this.ProID = item.ProID,
          name = item.name
        break
      }
    }
    this.cityArrayDemo = []
   
    for (var i = 0; i < province.cityArray.length; ++i) {
      var item = province.cityArray[i]
      if (item.ProID == this.ProID) {
        this.cityArrayDemo.push(item.name)
      }
    }

    if (this.data.provinceId != this.ProID) {

      this.setData({
        provinceId: this.ProID,
        provinceName: name,
        provinceIndex: e.detail.value,
        cityArray: this.cityArrayDemo,
        cityIndex: '',
        countyIndex: '',
        cityId: '',
        areaId: '',
        cityName: '',
        countyName: '',
      })
    } else {

      this.setData({
        provinceId: this.ProID,
        provinceName: name,
        provinceIndex: e.detail.value,
        cityArray: this.cityArrayDemo,
      })

    }
  },

  bindCityChange: function (e) {
 
    var name;
    for (var j = 0; j < province.cityArray.length; ++j) {
      var item = province.cityArray[j]
      if (item.name == this.data.cityArray[e.detail.value]) {
        this.CityID = item.CityID,
          name = item.name
        break
      }
    }

    this.countyArrayDemo = []
    for (var i = 0; i < province.countyArray.length; ++i) {
      var item = province.countyArray[i]
      if (item.CityID == this.CityID) {
        this.countyArrayDemo.push(item.DisName)
      }
    }

    if (this.data.cityId != this.CityID) {
      this.setData({
        cityId: this.CityID,
        cityIndex: e.detail.value,
        cityName: name,
        countyArray: this.countyArrayDemo,
        countyIndex: '',
        areaId: '',
        countyName: '',
      })

    } else {
      this.setData({
        cityId: this.CityID,
        cityIndex: e.detail.value,
        cityName: name,
        countyArray: this.countyArrayDemo,
      })

    }



  },

  bindCountyChange: function (e) {
    var name;

    for (var i = 0; i < province.countyArray.length; ++i) {
      var item = province.countyArray[i]
      if (item.DisName == this.data.countyArray[e.detail.value]) {
        this.AreID = item.Id,
          name = item.DisName
        break
      }
    }

    this.setData({
      areaId: this.AreID,
      countyIndex: e.detail.value,
      countyName: name,
    })
  },


})